package com.jzg.jcptadmin.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.adpter.SectionsPagerAdapter;
import com.jzg.jcptadmin.app.ActivityHelp;
import com.jzg.jcptadmin.base.BaseActivity;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import butterknife.OnPageChange;

/**
 * Created by wangyd on 15/12/28.
 * 及时评估界面包含快速评估和Vin码查询界面
 */
public class TimelyEstimateActivity extends BaseActivity implements View.OnClickListener, View.OnFocusChangeListener, ViewPager.OnPageChangeListener {

    @Bind(R.id.container)
    ViewPager viewPager;

    @Bind(R.id.title_content)
    TextView title_content;
    @Bind(R.id.title_return)
    ImageView title_return;
    @Bind(R.id.title_history)
    ImageView title_history;

    @Bind(R.id.dollars_btn)
    TextView dollars_btn;
    @Bind(R.id.zoom_btn)
    TextView zoom_btn;

    private SectionsPagerAdapter sectionsPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timely_rstimate_layout);
        ButterKnife.bind(this);
        initView();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    private void initView() {
        title_content.setText(getResources().getString(R.string.rapid_evaluator_content));
        sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(sectionsPagerAdapter);
        //设置默认项颜色，避免第二页焦距被抢，造成文本框首次获取不到标记
        dollars_btn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.dollarsblue), null, null);
        dollars_btn.setTextColor(getResources().getColor(R.color.blue));
//        dollars_btn.requestFocus();

    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    @OnClick({R.id.title_return, R.id.title_history})
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.title_return:
                cancelFocusChangeListener();
                finish();
                break;
            case R.id.title_history:
                toHistoryActivity();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            Logger.i("onkeyDown");
            cancelFocusChangeListener();
            finish();
        }
        return true;
    }

    /**
     * 取消焦距监听
     */
    public void cancelFocusChangeListener() {
        //避免返回关闭时出现ViewPager空
        dollars_btn.setOnFocusChangeListener(null);
        zoom_btn.setOnFocusChangeListener(null);
    }


    public void toHistoryActivity() {
        ActivityHelp.startActivity(this, HistoryActivity.class);
    }

    /**
     * Called when the focus state of a view has changed.
     *
     * @param v        The view whose state has changed.
     * @param hasFocus The new focus state of v.
     */
    @Override
    @OnFocusChange({R.id.dollars_btn, R.id.zoom_btn})
    public void onFocusChange(View v, boolean hasFocus) {
        if (viewPager == null && !hasFocus) return;
        switch (v.getId()) {
            case R.id.dollars_btn:
                viewPager.setCurrentItem(SectionsPagerAdapter.ONE_PAGE);
                MobclickAgent.onEvent(TimelyEstimateActivity.this,"pinggushi_ershoucheguzhi");//友盟统计
                break;
            case R.id.zoom_btn:
                viewPager.setCurrentItem(SectionsPagerAdapter.TWO_PAGE);
                MobclickAgent.onEvent(TimelyEstimateActivity.this,"pinggushi_vinchaxun");//友盟统计
                break;
        }
    }

    /**
     * This method will be invoked when the current page is scrolled, either as part
     * of a programmatically initiated smooth scroll or a user initiated touch scroll.
     *
     * @param position             Position index of the first page currently being displayed.
     *                             Page position+1 will be visible if positionOffset is nonzero.
     * @param positionOffset       Value from [0, 1) indicating the offset from the page at position.
     * @param positionOffsetPixels Value in pixels indicating the offset from position.
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /**
     * This method will be invoked when a new page becomes selected. Animation is not
     * necessarily complete.
     *
     * @param position Position index of the new selected page.
     */
    @Override
    @OnPageChange(R.id.container)
    public void onPageSelected(@NonNull int position) {
//        Logger.i(position + "");
//        在手动滑动pager的时候重新请求焦点(无效)
//   TODO     原始方式填充以后做优化
        if (position == SectionsPagerAdapter.ONE_PAGE) {
//            dollars_btn.requestFocus();
            dollars_btn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.dollarsblue), null, null);
            dollars_btn.setTextColor(getResources().getColor(R.color.blue));
            zoom_btn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.zoomhui), null, null);
            zoom_btn.setTextColor(getResources().getColor(R.color.grey3));
        } else if (position == SectionsPagerAdapter.TWO_PAGE) {
//            zoom_btn.requestFocus();
            zoom_btn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.zoomblue), null, null);
            zoom_btn.setTextColor(getResources().getColor(R.color.blue));
            dollars_btn.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.drawable.dollarsgray), null, null);
            dollars_btn.setTextColor(getResources().getColor(R.color.grey3));
        }
    }

    /**
     * Called when the scroll state changes. Useful for discovering when the user
     * begins dragging, when the pager is automatically settling to the current page,
     * or when it is fully stopped/idle.
     *
     * @param state The new scroll state.
     * @see ViewPager#SCROLL_STATE_IDLE
     * @see ViewPager#SCROLL_STATE_DRAGGING
     * @see ViewPager#SCROLL_STATE_SETTLING
     */
    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
